package io.renren.common.utils;

import io.renren.modules.bs.vo.ExtraPatientVo;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class MathUtil {

    public static List<ExtraPatientVo> getRandomList(int num, List<ExtraPatientVo> list) {

        Integer[] randowNum = getRandowNum(num, list.size());
        List<ExtraPatientVo> extraPatientVos = new ArrayList<>();
        for (int i = 0; i < randowNum.length; i++) {

            extraPatientVos.add(list.get(randowNum[i]));
        }
//        list.removeAll(extraPatientVos);
        remove(list, extraPatientVos);
        return extraPatientVos;
    }

    public static void remove(List<ExtraPatientVo> list, List<ExtraPatientVo> extraPatientVos) {
        Set<Long> collect = extraPatientVos.stream().map(ExtraPatientVo::getDepartmentPatientId).collect(Collectors.toSet());
        Iterator<ExtraPatientVo> it = list.iterator();
        while (it.hasNext()) {
            ExtraPatientVo next = it.next();
            if (collect.contains(next.getDepartmentPatientId())) {
                it.remove();
            }
        }
    }

    public static void remove(List<ExtraPatientVo> list, Long departmentPatientId) {
        Iterator<ExtraPatientVo> it = list.iterator();
        while (it.hasNext()) {
            ExtraPatientVo next = it.next();
            if (departmentPatientId==next.getDepartmentPatientId()) {
                it.remove();
            }
        }
    }

    private static Integer[] getRandowNum(int num, int limit) {

        int[] redBall = new int[limit];

        for (int i = 0; i < redBall.length; i++) {
            redBall[i] = i + 1;

        }

        Integer[] redNumber = new Integer[num];     //存储六个随机数的实际数组
        int index = -1;                             //通过随机数字数组下标获取随机数
        for (int i = 0; i < redNumber.length; i++) {

            index = new Random().nextInt(redBall.length - i);   //每次获取数字数组长度-i的随机数，比如第一次循环为33第二次为32，
            redNumber[i] = redBall[index];                   //把数字数组随机下标的值赋给实际数组
            int temp = redBall[index];                           //定义一个变量暂存下标为index时的值
            redBall[index] = redBall[redBall.length - 1 - i];  //把下标为index的值与数组下标最后的值交换
            redBall[redBall.length - 1 - i] = temp;             //交换后，下次循环把数字数组最后的值去掉，从而实现不重复
        }
        //由于要下标，减一
        for (int i = 0; i < redNumber.length; i++) {
            redNumber[i] = redNumber[i] - 1;
        }
        return redNumber;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            Integer[] randowNum = getRandowNum(10, 10);
            System.out.println(StringUtils.join(randowNum, ","));
        }
    }
}
